<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggleDialog" :class="{ active }">
      <span class="placeholder">{{address.fullLocation?address.fullLocation:'请选择配送地址'}}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <div v-if="!list" class="loading"></div>
      <template v-else>
        <span class="ellipsis" v-for="item in list" :key="item.id" @click="filtCity(item.areaList,item)">{{item.name}}</span>
      </template>
    </div>
  </div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
export default {
  name: 'XtxCity',
  setup (props, { emit }) {
    // 控制展开收起,默认收起
    const address = reactive({
      provinceCode: '', // 所在省份编码
      provinceName: '',
      cityCode: '', // 所在城市编码
      cityName: '',
      countyCode: '', // 所在的县区编码
      countyName: ''
      // fullLocation: ''// 完整的地址
    })
    const active = ref(false)
    const list = ref(false)
    const getCityData = async () => {
      if (!window.CityData) {
        const { data } = await axios('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
        window.CityData = data
      }
      return window.CityData
    }
    const filtCity = (city, parent) => {
      if (parent) {
        if (parent.level === 0) {
          address.provinceCode = ''
          address.provinceName = ''
          address.cityCode = ''
          address.cityName = ''
          address.countyCode = ''
          address.countyName = ''
          address.fullLocation = false
          address.provinceCode = parent.code
          address.provinceName = parent.name
        }
        if (parent.level === 1) {
          address.cityCode = parent.code
          address.cityName = parent.name
        }
        if (parent.level === 2) {
          address.countyCode = parent.code
          address.countyName = parent.name
          for (const key in address) {
            if (address[key] !== '') {
              address.fullLocation = address.provinceName + ' ' + address.cityName + ' ' + address.countyName
            }
          }
        }
      }
      if (!city) active.value = false
      list.value = city
    }
    watch(() => address, () => {
      if (address.fullLocation) {
        emit('address', address)
      }
    })
    const openDialog = async () => {
      const data = await getCityData()
      filtCity(data)

      active.value = true
    }
    const closeDialog = () => {
      active.value = false
    }
    // 切换展开收起
    const toggleDialog = () => {
      if (active.value) closeDialog()
      else openDialog()
    }
    // 点击其他位置隐藏
    const target = ref(null)
    onClickOutside(target, () => {
      closeDialog()
    })
    return { active, toggleDialog, target, list, filtCity, address }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
